package com.haidechizi.i18n.spring.boot.autoconfigure;

import com.haidechizi.i18n.common.utils.I18nUtils;
import com.haidechizi.i18n.spring.boot.properties.I18nProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ResourceBundleMessageSource;

@Configuration
@ConditionalOnClass(ResourceBundleMessageSource.class)
@EnableConfigurationProperties(I18nProperties.class)
public class I18nMessageAutoConfiguration {

    @Autowired
    private I18nProperties i18nProperties;

    @Bean
    @ConditionalOnMissingBean
    public ResourceBundleMessageSource resourceBundleMessageSource() {
        ResourceBundleMessageSource source = new ResourceBundleMessageSource();
        source.setBasenames(i18nProperties.getI18nMessage());
        source.setDefaultEncoding(i18nProperties.getDefaultEncoding());
        return source;
    }


    @Bean
    @ConditionalOnMissingBean
    public I18nUtils i18nUtils() {
        return new I18nUtils(resourceBundleMessageSource());
    }


}
